<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 配置
//-------------------------

namespace app\admin\controller;

use app\admin\Controller;
use think\Db;


class TaskConfig extends Controller
{
    //行业列表
    public function index()
    {
        $list = Db::name('task_first_classfy')->order('id desc')->select();
        return view('task_config/index', [
            'list' => $list
        ]);
    }

    //新增
    public function add()
    {
        if ($this->request->post()) {
            $data = input('post.');
            $img = getImgUrl($data['img'],'task_config');
            //$case_img = getImgUrl($data['case_img'],'task_config');

            $info = [
                'img' => $img,
                'title' => $data['title'],
                'subtitle' => $data['subtitle'],
                //'case_img' => $case_img,
                'price' => $data['price'],
                'num' => $data['num'],
                //'screenshots_num' => $data['screenshots_num']
            ];
            $result = Db::name('task_first_classfy')->insert($info);
            if (!$result) {
                return ajax_return_error('失败');
            }
            return ajax_return_adv('成功');
        } else {
            return view('task_config/add');
        }
    }


    //编辑
    public function edit()
    {
        if ($this->request->post()) {
            $data = input('post.');
            $img = getImgUrl($data['img'],'task_config');
            //$case_img = getImgUrl($data['case_img'],'task_config');
            $info = [
                'img' => $img,
                'title' => $data['title'],
                'subtitle' => $data['subtitle'],
                //'case_img' => $case_img,
                'price' => $data['price'],
                'num' => $data['num'],
                //'screenshots_num' => $data['screenshots_num']
            ];
            $result = Db::name('task_first_classfy')->where(['id' => $data['id']])->update($info);
            if ($result === false) {
                return ajax_return_error('失败');
            }
            return ajax_return_adv('成功');
        } else {
            $info = Db::name('task_first_classfy')->where(['id' => input('id')])->find();
            return view('task_config/edit', ['info' => $info]);
        }
    }

    //删除
    public function delete(){
        $result = Db::name('task_first_classfy')->where(['id' => input('id')])->delete();
        if (!$result) {
            return ajax_return_error('失败');
        }
        return ajax_return_adv('成功');
    }
}